package com.jinduplan.planmanager.mvp.model

import com.cxz.kotlin.baselibs.mvp.BaseModel
import com.cxz.kotlin.baselibs.utils.CommonUtil
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.google.gson.JsonArray
import com.jinduplan.planmanager.bean.*
import com.jinduplan.planmanager.http.MainRetrofit
import com.jinduplan.planmanager.mvp.contract.MainContract
import com.jinduplan.planmanager.mvp.contract.ProjectContract
import com.jinduplan.planmanager.mvp.contract.ProjectUploadContract
import com.jinduplan.planmanager.mvp.contract.ProjectUploadDetailContract
import com.jinduplan.planmanager.utils.toMap
import io.reactivex.rxjava3.core.Observable
import org.json.JSONArray
import org.json.JSONObject

/**
 * @author admin
 * @date 2018/11/20
 * @desc
 */
class ProjectUploadDetailModel : BaseModel(), ProjectUploadDetailContract.Model {
    override fun getProjectUploadDetailList(project_id: Int): Observable<HttpResult<ProjectUploadDetailBean>> {
        val map = HashMap<String,Int>()
        map["project_id"] = project_id
        return MainRetrofit.service.getProjectUploadDetailList(GsonHelper.getRequestIntBoby(map))
    }

    override fun getNewProjectUploadDetailList(project_id: Int): Observable<HttpResult<NewProjectUploadDetailBean>> {
        val map = HashMap<String,Int>()
        map["project_id"] = project_id
        return MainRetrofit.service.getNewProjectUploadDetailList(GsonHelper.getRequestIntBoby(map))
    }

    override fun getProjectLabourList(project_id: Int): Observable<HttpResult<ProjectLabour>> {
        val map = HashMap<String,Int>()
        map["project_id"] = project_id
        return MainRetrofit.service.getProjectLabourList(GsonHelper.getRequestIntBoby(map))
    }

    override fun getProjectWorkmanShipList(page:Int,limit:Int,hanging_blue_id:Int,
                                           project_id:Int,building_id:Int,
                                           workmanship_id:Int,facade_id:Int): Observable<HttpResult<ProjectWorkmanShip>> {
        val map = HashMap<String,Int>()
        map["page"] = page
        map["limit"] = limit
        map["hanging_blue_id"] = hanging_blue_id
        map["project_id"] = project_id
        map["building_id"] = building_id
        if(workmanship_id!=0){
            map["workmanship_id"] = workmanship_id
        }
        map["facade_id"] = facade_id
        return MainRetrofit.service.getProjectWorkmanShipList(GsonHelper.getRequestIntBoby(map))
    }

    override fun putProjectUpload(jsonData: RequestPutProjectBean): Observable<HttpListResult<ProjectUploadSuccessBean>> {
        val map = HashMap<String,Any>()
        map["completion_date"] = jsonData.completion_date
        map["project_id"] = jsonData.project_id
        var userJsonArray=JSONArray()
        for (i in 0 until jsonData.user_arr.size){
            var jsonObject= JSONObject()
            jsonObject.put("labour_id",jsonData.user_arr[i].user_id)
            var constructionJsonArray=JSONArray()
            for (j in 0 until jsonData.user_arr[i].construction_arr.size){
                var jsonObject2= JSONObject()
                jsonObject2.put("construction_id",jsonData.user_arr[i].construction_arr[j].construction_id)
                jsonObject2.put("completion_area",jsonData.user_arr[i].construction_arr[j].completion_area)
                jsonObject2.put("usage_amount",jsonData.user_arr[i].construction_arr[j].usage_amount)
                constructionJsonArray.put(jsonObject2)
            }
            jsonObject.put("construction_arr",constructionJsonArray)
            userJsonArray.put(jsonObject)

        }
        map["user_arr"] = userJsonArray
        return MainRetrofit.service.putProjectUpload(GsonHelper.getRequestObjectBoby(map))
    }


}